function publishInventory(){
	var pic_title=$("#pic_title").prop("files")[0]
	var pic_a=$("#pic_a").prop("files")[0]
	var pic_b=$("#pic_b").prop("files")[0]
	var pic_c=$("#pic_c").prop("files")[0]
	var pic_d=$("#pic_d").prop("files")[0]

	var data=new FormData()
	data.append("pic_title",pic_title)
	data.append("pic_a",pic_a)
	data.append("pic_b",pic_b)
	data.append("pic_c",pic_c)
	data.append("pic_d",pic_d)
	var _data={}
	try{
		var t=window.opener.publish_inventory
		id=t.id
		_data.id=id
	}
	catch(e){
	}
	_data.name=document.getElementById("title").value;
	_data.test_id=$("#test").attr("sid")
	if(/^ *$/.test(_data.name)){
		showErrorInfo("题目不能为空!")
		return false;
	}
	if(_data.test_id===undefined){
		showErrorInfo("您还没有选择实验！")
		return false;
	}
	_data.type=getQ().type
	_data.question_a=document.getElementById("a").value;
	_data.question_b=document.getElementById("b").value;
	_data.question_c=document.getElementById("c").value;
	_data.question_d=document.getElementById("d").value;
	_data.answer=[]
	$(".glyphicon-check").each(function(){
		var v=this.getAttribute("value");
		_data.answer.push(v)
	})
	_data.answer=JSON.stringify(_data.answer)
	_data.value=document.getElementById("score").value
	_data=JSON.stringify(_data)
	data.append("data",_data)
	jQuery.ajax({
		"url":"/inventory/",
		"type":"POST",
		"data":data,
		"processData":false,
                "contentType": false,
		"error":errorAjax,
		"complete":stop_load_pic,
		"beforeSend":start_load_pic,
                "cache": false,
		"success":function(data){
			data=JSON.parse(data)
			if(!data.status){
				showErrorInfo(data.content)
				return false
			}
			else{
				alert("操作成功！")
				delete window.opener.publish_inventory
				window.opener.location.reload()
				window.close();
			}
		}
	})
}

function parseData(){
	if(getQ().type==="判断题"){
		document.getElementById("hide-c").style.display="none";
		document.getElementById("hide-d").style.display="none";
		document.getElementById("hide-cc").style.display="none";
		document.getElementById("hide-dd").style.display="none";
	}
	try{
		_data=window.opener.publish_inventory
		console.log(_data)
		//$("#test_id").find("option[value="+"'"+test_id+"'"+"]").attr("selected",true)
		var t=document.getElementById("test")
		t.setAttribute("sid",_data.test_id)
		t.setAttribute("value",_data.content)
		document.getElementById("title").value=_data.name;
		document.getElementById("a").value=_data.a;
		document.getElementById("b").value=_data.b;
		document.getElementById("c").value=_data.c;
		document.getElementById("d").value=_data.d;
		document.getElementById("score").value=_data.value;
			
		if (_data.pic_title===""){
			$("#ttt-title").text("已插图片").css({"background":"green","color":"white"})
		}
		if(_data.pic_a===""){
			$("#ttt-a").text("已插图片").css({"background":"green","color":"white"})
		}
		if(_data.pic_b===""){
			$("#ttt-b").text("已插图片").css({"background":"green","color":"white"})
		}
		if(_data.pic_c===""){
			$("#ttt-c").text("已插图片").css({"background":"green","color":"white"})
		}
		if(_data.pic_d===""){
			$("#ttt-d").text("已插图片").css({"background":"green","color":"white"})
		}
		var answer=_data.answer
		answer=JSON.parse(_data.answer)
		$("span[type='checkbox']").each(function(){
			var id=this.getAttribute("value")
			var e=this
			for(var x=0;x<answer.length;x++){
				if(id===answer[x]){
					$(e).removeClass("glyphicon-unchecked").addClass("glyphicon-check")
				}
			}
		})
	}
	catch(e){
		console.log("不加载页面数据")
	}
	
}
document.getElementById("confirm").onclick=function(){
	publishInventory()
}
$(document).on("click","span[type='checkbox']",function(){
	var type=getQ().type
	if(type==="单选题"){
		if($(this).hasClass("glyphicon-unchecked")){
			$("span[type='checkbox']").removeClass("glyphicon-check").addClass("glyphicon-unchecked")
			$(this).removeClass("glyphicon-unchecked").addClass("glyphicon-check")
		}
		else{
			$("span[type='checkbox']").removeClass("glyphicon-check").addClass("glyphicon-unchecked")
		}
	}
	else if(type==="多选题"){
		if($(this).hasClass("glyphicon-unchecked")){
			$(this).removeClass("glyphicon-unchecked").addClass("glyphicon-check")
		}
		else{
			$(this).removeClass("glyphicon-check").addClass("glyphicon-unchecked")
		}
	}
	else if(type==="判断题"){
		if($(this).hasClass("glyphicon-unchecked")){
			$("span[type='checkbox']").removeClass("glyphicon-check").addClass("glyphicon-unchecked")
			$(this).removeClass("glyphicon-unchecked").addClass("glyphicon-check")
		}
		else{
			$("span[type='checkbox']").removeClass("glyphicon-check").addClass("glyphicon-unchecked")
		}
	}
})
parseData()
document.getElementById("pic_title").onchange=function(){
	$("#ttt-title").css({"background":"green","color":"white"}).text("已插图片")
}
document.getElementById("pic_a").onchange=function(){
	$("#ttt-a").css({"background":"green","color":"white"}).text("已插图片")
}
document.getElementById("pic_b").onchange=function(){
	$("#ttt-b").css({"background":"green","color":"white"}).text("已插图片")
}
document.getElementById("pic_c").onchange=function(){
	$("#ttt-c").css({"background":"green","color":"white"}).text("已插图片")
}
document.getElementById("pic_d").onchange=function(){
	$("#ttt-d").css({"background":"green","color":"white"}).text("已插图片")
}
document.getElementById("test").onclick=function(){
        window.open ("search_exp.html", "_blank", "height=700, width=1100, top=160, left=200, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, status=no");
	
}

